﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        
        static List<Student> list = new List<Student>();
        static Hashtable ht = new Hashtable();
        static Student s1 = new Student("sdf","sdf",123);
        static Student s2 = new Student("swdf", "sdrgf", 13);
        static Student s3 = new Student("sddf", "srdf", 14);
        static Student s4 = new Student("sdf", "sderrf", 23);
        static Student s5 = new Student("svfdf", "sdf", 1023);
        static void Main(string[] args)
        {
            list.Add(s1);
            list.Add(s2);
            list.Add(s3);
            list.Add(s4);
            list.Add(s5);
            ht.Add(s1.age, s1);
            ht.Add(s2.age, s2);
            ht.Add(s3.age, s3);
            ht.Add(s4.age, s4);
            ht.Add(s5.age, s5);
            Select();

        }
        
        
        public static void Select() 
        {
            Console.WriteLine("请选择：1，添加学生信息 2，删除学生信息 3，查询学生信息");
            int key = int.Parse(Console.ReadLine());
            switch (key)
            {
                case 1:
                    Add();
                    break;
                case 2:
                    Delete();
                    break;
                case 3:
                    Caxun();
                    break;
                default:

                    break;
            }

        }

        private static void Caxun()
        {
           
            Console.WriteLine("1,查询所有（学号排序）2，查询所有（姓名排序）3，查询所有（年龄排序 ）4，学号查询5，退出");
            int a = int.Parse(Console.ReadLine());
            switch (a)
            {
                case 1:
                    list.Sort(new StudentNum());
                    list.Reverse();
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    Caxun();
                    break;
                case 2:
                    list.Sort(new Agesore());
                    list.Reverse();
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    Caxun();
                    break;
                case 3:
                    list.Sort(new Agesore());
                    list.Reverse();
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    Caxun();
                    break;
                case 4:
                    Console.WriteLine("请输入学号");
                    int num = int.Parse(Console.ReadLine());
                    if (ht.ContainsKey(num))
                    {
                        Console.WriteLine(ht[num]);
                        Caxun();
                    }
                   
                    else
                    {
                        Console.WriteLine("已存在");
                        Caxun();
                    }
                    
                    
                    break;
                case 5:
                    Console.WriteLine("结束");
                    break;
                default:
                    break;
            }
        }

        private static void Delete()
        {
            Console.WriteLine("请输入要删除的学号");
            int a = int.Parse(Console.ReadLine());
            if (ht.Contains(a))
            {
                list.Remove((Student) ht[a]);
                ht.Remove(a);
                
                Console.WriteLine("成功");
            }
            Select();
        }

        private static void Add()
        {
            Console.WriteLine("请输入姓名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入学号");
            string num = Console.ReadLine();
            Console.WriteLine("请输入年龄");
            int age = int.Parse(Console.ReadLine());
            Student student = new Student(name, num, age);

            if (ht.ContainsKey(num))
            {
                Console.WriteLine("已存在");
                Select();
            }
            else
            {
                list.Add(student);
                ht.Add(student.name, student);
                Select();
            }
        }
    }
}
